package com.neusoft.nep.common;

import com.neusoft.nep.admin.entity.AqiData; // Make sure to import AqiData
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class AqiDataFileUtil {

    private static final String FILE_PATH = "fankui.txt"; // Define your file path

    /**
     * Reads AQI data from the specified file.
     * @return A list of AqiData objects read from the file.
     */
    public static List<AqiData> readAqiDataFromFile() {
        List<AqiData> data = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 8) {
                    data.add(new AqiData(
                            parts[0].trim(), // id
                            parts[1].trim(), // province
                            parts[2].trim(), // city
                            parts[3].trim(), // level
                            parts[4].trim(), // date
                            parts[5].trim(), // time
                            parts[6].trim(), // gridStaff
                            parts[7].trim()  // reporter
                    ));
                } else {
                    System.err.println("Skipping malformed line in " + FILE_PATH + ": " + line);
                }
            }
        } catch (IOException e) {
            System.err.println("Error reading AQI data from " + FILE_PATH + ": " + e.getMessage());
            // Optionally, rethrow or handle more gracefully, e.g., show a dialog
        }
        return data;
    }


    public static void writeAqiDataToFile(List<AqiData> aqiDataList) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH))) {
            for (AqiData data : aqiDataList) {
                String line = String.join(",",
                        data.getId(),
                        data.getProvince(),
                        data.getCity(),
                        data.getLevel(),
                        data.getDate(),
                        data.getTime(),
                        data.getGridStaff(),
                        data.getReporter()
                );
                writer.write(line);
                writer.newLine();
            }
            System.out.println("Data successfully written to: " + FILE_PATH + " (file created or overwritten)");
        } catch (IOException e) {
            System.err.println("Error writing data to " + FILE_PATH + ": " + e.getMessage());
            e.printStackTrace();
        }
    }


    public static void appendAqiDataToFile(List<AqiData> aqiDataList) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true))) { // 'true' for append mode
            for (AqiData data : aqiDataList) {
                String line = String.join(",",
                        data.getId(),
                        data.getProvince(),
                        data.getCity(),
                        data.getLevel(),
                        data.getDate(),
                        data.getTime(),
                        data.getGridStaff(),
                        data.getReporter()
                );
                writer.write(line);
                writer.newLine();
            }
            System.out.println("Data successfully appended to: " + FILE_PATH + " (file created or appended)");
        } catch (IOException e) {
            System.err.println("Error appending data to " + FILE_PATH + ": " + e.getMessage());
            e.printStackTrace();
        }
    }
}